package com.yang.frost.blueberry.controller;

import com.alibaba.fastjson.JSONObject;
import com.yang.frost.blueberry.config.annotation.Log;
import com.yang.frost.blueberry.config.annotation.OperationLogIgnore;
import com.yang.frost.blueberry.domain.SysPermission;
import com.yang.frost.blueberry.params.BaseParam;
import com.yang.frost.blueberry.params.LoginParam;
import com.yang.frost.blueberry.service.LoginService;
import com.yang.frost.blueberry.service.SysRolePermissionService;
import com.yang.frost.blueberry.util.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 10296
 * @date 2021/1/13
 * @Description: 登录管理类
 */
@RestController
@RequestMapping("/systemApi")
@Api(value = "系统登录API", tags = {"系统登录"})
public class LoginController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LoginService loginService;
    @Autowired
    private SysRolePermissionService rolePermissionServer;


    @Log("登录")
    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "系统用户登录")
    public ApiResult login(@Validated @RequestBody LoginParam loginParam, HttpServletResponse response) throws Exception {
        logger.info(loginParam + "");

        return loginService.login(loginParam);
    }

    /**
     * 验证token
     *
     * @param param    参数
     * @param response 返回
     * @return ""
     */
    @Log("验证Token")
    @PostMapping("/verifyAuthToken")
    @ApiOperation(value = "验证身份", notes = "验证Token")
    public ApiResult verifyAuthToken(@Validated @RequestBody BaseParam param, HttpServletResponse response) {

        logger.info(param.getToken());
        return loginService.verifyAuthToken(param.getToken());
    }


    @Log("获取系统用户信息")
    @PostMapping("/getSysInfo")
    @ApiOperation(value = "获取系统用户信息", notes = "获取系统用户信息")
    public ApiResult getSysInfo(@Validated @RequestBody LoginParam loginParam, HttpServletResponse response) throws Exception {

        //TODO  这个地方根据用户的Token 验证是否过期  然后返回用户信息

        return ApiResult.ok("");
    }

    @Log("获取菜单路由信息")
    @PostMapping("/getRoutersMenu")
    @ApiOperation(value = "获取菜单路由信息", notes = "获取菜单路由信息")
    public ApiResult getRoutersMenu(@Validated @RequestBody BaseParam param, HttpServletResponse response) throws Exception {
        //TODO 根据 token 获取用户信息
        List<SysPermission> menuTreeByUserId = rolePermissionServer.getMenuTreeByUserId(param.getId());
        logger.info(menuTreeByUserId + "");
        Object rows = JSONObject.toJSON(menuTreeByUserId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("rows", rows);


        return ApiResult.ok(resultMap);
    }


    /**
     * 重置密码
     *
     * @param loginParam 参数
     * @param response   ''
     * @return 对象
     * @throws Exception e
     */
    @PostMapping("/reset")
    public ApiResult<String> reset(@Validated @RequestBody LoginParam loginParam, HttpServletResponse response) throws Exception {

        return ApiResult.ok("密码重置成功");
    }

    /**
     * 忘记密码
     *
     * @param loginParam 参数
     * @param response   ''
     * @return 对象
     * @throws Exception e
     */
    @PostMapping("/forget")
    public ApiResult<String> forgetPassword(@Validated @RequestBody LoginParam loginParam, HttpServletResponse response) throws Exception {




        return ApiResult.ok("密码修改成功");
    }


    @PostMapping("/logout")
    @OperationLogIgnore
    public ApiResult<String> logout(@Validated @RequestBody BaseParam loginParam, HttpServletResponse response) throws Exception {

        loginService.logout(loginParam);


        return ApiResult.ok("退出成功");
    }
}
